package org.medusa.buffer;

public class BufferHandler extends Buffer {
	protected byte[] databuf = null;
	protected int buffer_size;
	protected int read_offset;
	protected int write_offset;

	public BufferHandler(byte[] buffer, int buffer_size, int data_size) {
		this.databuf = buffer;
		this.buffer_size = buffer_size;
		this.read_offset = 0;
		this.write_offset = data_size;
	}

	@Override
	public void Clear() {
		this.read_offset = this.write_offset = 0;
	}
	
	@Override
	public boolean IsFull() {
		return (this.write_offset == this.buffer_size);
	}
	
	@Override
	public boolean IsEmpty() {
		return (this.write_offset == 0);
	}

	// size
	@Override
	public int BufferSize() {
		return this.buffer_size;
	}

	@Override
	public int DataSize() {
		return this.write_offset - this.read_offset;
	}

	@Override
	public int FreeSize() {
		return this.buffer_size - this.write_offset;
	}

	@Override
	public int LineDataSize() {
		return this.DataSize();
	}

	@Override
	public int LineFreeSize() {
		return this.FreeSize();
	}

	// Byte
	@Override
	public byte ReadInt8() {
		byte ret = this.databuf[this.read_offset];
		this.read_offset += 1;
		return ret;
	}

	@Override
	public void WriteInt8(byte val) {
		this.databuf[this.write_offset] = val;
		this.write_offset += 1;
	}

	// Bytes
	public byte[] GetBytes() {
		return this.databuf;
	}

	public int GetReadOffset() {
		return this.read_offset;
	}

	public int GetWriteOffset() {
		return this.write_offset;
	}

}
